<?php

    namespace Logistics\Controller;

    use Common\Controller\AdminbaseController;

    class AdminDriverController extends AdminbaseController {

        protected $m_driver,$m_logistics;

        function _initialize() {
            parent::_initialize();
            $this->m_driver= D("Logistics/Driver");
            $this->m_logistics = M("Logistics");
        }

        /**
         * 驾驶员管理
         */
        public function index(){
            if(is_manager($this->user_model)){
                $where = array();
            }else{
                $where = array('d.lid'=>get_logistics_id());
            }

            $count=$this->m_driver->alias("d")->where($where)->count();
            $page = $this->page($count, 15);
            $list = $this->m_driver->field("d.*,l.logistics_name")->alias("d")->where($where)
                ->join("bdt_logistics l ON l.id = d.lid")
                ->order("create_time desc")
                ->limit($page->firstRow , $page->listRows)
                ->select();


            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            $this->display();
        }

        /**
         * 排序
         */
        public function listorders(){
            $status = parent::_listorders($this->m_driver,'sort');
            if ($status) {
                $this->success("排序更新成功！");
            } else {
                $this->error("排序更新失败！");
            }
        }

        // 添加驾驶员
        public function add(){
            $id = I("get.id",0,'intval');
            if(!empty($id)){
                $item=$this->m_driver->where(array('id'=>$id))->find();
                $this->assign("item",$item);
            }
            if(is_manager($this->user_model)){
                $logistics_list = $this->m_logistics->select();
                $this->assign("logistics_list",$logistics_list);
            }

            $this->display();
        }

        // 文章添加提交
        public function add_post(){
            if (IS_POST) {
                $id = I("post.id");

                $insert = I("post.");
                if($this->user_model == 4 || session("ADMIN_ID") == 10){
                    $insert['lid'] = $insert["lid"];
                }else{
                    $insert['lid'] = get_logistics_id();
                }



                if(empty($id)){
                    $insert['create_time'] = time();
                    $result =$this->m_driver->add($insert);
                }else{
                    $insert['create_time'] =time();
                    $result = $this->m_driver->where(array("id"=>$id))->save($insert);
                }
                if ($result) {
                    $this->success("添加成功！");
                } else {
                    $this->error("添加失败！");
                }

            }
        }




        // 文章删除
        public function delete(){
            if(isset($_GET['id'])){
                $id = I("get.id",0,'intval');
                if ($this->m_driver->where(array('id'=>$id))->delete() !==false) {
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }else{
                $this->error("参数有误！");
            }

        }

    }